
class FlazXPlugin:
	def __init__(self):
		# put all the links this plugins supports here - it will be used to filter supported URLs
		self.supported_sites = ['www.flazx.com']
		self.supported_urls = ['www.flazx.com/ebook']
		self.cookies_to_block = []
		# whether we support pause/resume
		self.supports_resume = False
		# maximum number of concurrent downloads
		self.max_concurrent = 1
		# parameter names we require
		self.param_names = []
		# parameter values
		self.param_vals = {}
		self.name = self.supported_sites[0]

	def get(self, url):
		"""Get a book from www.flazx.com"""

		data = ge_url_data(url)
		parser = URLLister()
		parser.feed(data)

		#get_links(url, 'www.flazx.com/selectpreview/')
		found_url = False
		for script_url in parser.urls:
			if 'www.flazx.com/selectpreview/' in script_url:
				url = script_url
				found_url = True
				break
		parser.close()
		if not found_url:
			raise CantDownload('No direct download link found')

		links = get_links(url, 'www.flazx.com/preview/')
		if len(links) != 1:
			raise CantDownload('No request download link found')

		url = None
		for line in get_url_data(links[0]).splitlines():
			if 'flazx.net/preview/' in line:
				url = get_url_from_line(line)
				break
		if not url:
			raise CantDownload('No download link found')

		get_next_line = False
		book_name = 'Unknown'
		year = 'None'
		for line in data.splitlines():
			if '<title>' in line:
				book_name = line.split('<title>FlazX - ')[1].split('<')[0].strip()
			if get_next_line:
				year = line.split('<')[0].split()[2]
				break
			if 'Release Date' in line:
				get_next_line = True
		del data
		book_name = get_valid_name(book_name)
		mydir = os.path.join(config.dl_dir, '%s (%s)' % (book_name.decode('iso-8859-1'), year))
		try: os.makedirs(mydir)
		except: pass

		local_file = get_filename_from_url(url)
		file_to_save = os.path.join(mydir, local_file)
		download_file_with_retries(file_to_save, url)
		extract_all(file_to_save)
		config.report.report_finish(local_file)

	def url_matches(self, url):
		"""Check whether we support this URL"""
		for l in self.supported_urls:
			if l in url:
				return True
		return False

	def version(self):
		"""Return plugin version - when interface changes, the version changes"""
		return 2

modules = [FlazXPlugin()]
